from rest_framework.exceptions import APIException
from rest_framework import status
from api.utils.enums import ErrorCode
from rest_framework.exceptions import _get_error_details


class BadException(APIException):
    status_code = status.HTTP_400_BAD_REQUEST


class InternalErrorException(APIException):
    pass


class ResourceNotFound(BadException):
    default_detail = ErrorCode.ResourceNotFound.display_string
    default_code = ErrorCode.ResourceNotFound.value


class CustomError(BadException):
    default_detail = ErrorCode.CustomError.display_string
    default_code = ErrorCode.CustomError.value


class ResourceExisted(BadException):
    default_detail = ErrorCode.ResourceExisted.display_string
    default_code = ErrorCode.ResourceExisted.value


class FieldError(Exception):
    default_code = "field_error"

    def __init__(self, detail, code=None):
        if code is None:
            code = self.default_code

        self.code = code
        self.detail = _get_error_details(detail, code)
        super(FieldError, self).__init__(self.detail)
